﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Text;
using System.Configuration;
using DB;
using Model;

/// <summary>
/// ArchivesDestinationRecordService 的摘要说明
/// </summary>
/// 
namespace ArchivesManagement
{
    public class ArchivesDestinationRecordService
    {
        string sql = string.Empty;
        ArchiveInformation archiveInformation = new ArchiveInformation();
        public ArchivesDestinationRecordService()
        {

        }

        //得到档案编号
        public string  GetFileNumber()
        {
            //ArchivesDestinationRecord archivesDestinationRecord= new ArchivesDestinationRecord();
            string  fileNumber = string .Empty;
            //string studentID = string.Empty;
            sql = "select * from ArchivesDestinationRecord where FileNumer =" + fileNumber.ToString();
            
            SqlDataReader dr = DBHelper.Exc(sql);
            if(dr.Read())
            {
                
                fileNumber = Convert.ToString(dr["FileNumber"]);
                

            }
            return fileNumber;
        }

        //根据档案编号得到档案去向信息集合
        public IList<ArchivesDestinationRecord> GetArchivesDestinationRecordByFileNumer(string fileNumber)
        {
            IList<ArchivesDestinationRecord> archivesDestinationRecords = new List<ArchivesDestinationRecord>();
            sql = "select * from ArchivesDestinationRecord";
            if (fileNumber.ToString() != null)
            {
                sql += "where FileNumber =" + fileNumber.ToString();
            }
            SqlDataReader dr = DB.DBHelper.Exc(sql);
            while (dr.Read())
            {
                ArchivesDestinationRecord archivesDestinationRecord = new ArchivesDestinationRecord();
                archivesDestinationRecord.ID = Convert.ToInt32(dr["ID"]);
                archivesDestinationRecord.FileNumber = Convert.ToString(dr["FileNumber"]);
                archivesDestinationRecord.ReceivingUnit = Convert.ToString(dr["RecivingUnit"]);
                archivesDestinationRecord.ReceivingUnitPhone = Convert.ToString(dr["ReceivingUnitPhone"]);
                archivesDestinationRecord.ReceivingTime = Convert.ToDateTime(dr["ReceivingTime"]);
                archivesDestinationRecord.UnitAddress = Convert.ToString(dr["UnitAddress"]);
                archivesDestinationRecords.Add(archivesDestinationRecord);

            }
            return archivesDestinationRecords;
        }

        /// <summary>
        /// 根据去向ID得到档案去向记录实体对象
        /// </summary>
        /// <returns></returns>
        /// 
        public ArchivesDestinationRecord GetAllArchivesDestinationRecordByID(int id)
        {
            ArchivesDestinationRecord archivesDestinationRecord = new ArchivesDestinationRecord();
            SqlDataReader dr = DB.DBHelper.Exc(sql);
            if (dr.Read())
            {
                //archivesDestinationRecord.ID = Convert.ToInt32(dr["ID"]);
                archivesDestinationRecord.FileNumber = Convert.ToString(dr["FileNumber"]);
                archivesDestinationRecord.ReceivingUnit = Convert.ToString(dr["ReceivingUnit"]);
                archivesDestinationRecord.ReceivingTime = Convert.ToDateTime(dr["ReceivingTime"]);
                archivesDestinationRecord.ReceivingUnitPhone = Convert.ToString(dr["ReceivingUnitPhone"]);
                archivesDestinationRecord.UnitAddress = Convert.ToString(dr["UnitAddress"]);

            }
            return archivesDestinationRecord;
        }

        //得到档案去向记录信息集合
        public IList<ArchivesDestinationRecord> GetArchivesDestinationRecord()
        {
            IList<ArchivesDestinationRecord> archivesDestinationRecords = new List<ArchivesDestinationRecord>();
            sql = "select * from ArchivesDestinationRecord";
            SqlDataReader dr = DB.DBHelper.Exc(sql);
            while (dr.Read())
            {
                ArchivesDestinationRecord archivesDestinationRecord = new ArchivesDestinationRecord();
                archivesDestinationRecord.ID = Convert.ToInt32(dr["ID"]);
                archivesDestinationRecord.FileNumber = Convert.ToString(dr["FileNumber"]);
                archivesDestinationRecord.ReceivingUnit = Convert.ToString(dr["ReceivingUnit"]);
                archivesDestinationRecord.ReceivingTime = Convert.ToDateTime(dr["ReceivingTime"]);
                archivesDestinationRecord.ReceivingUnitPhone = Convert.ToString(dr["ReceivingUnitPhone"]);
                archivesDestinationRecord.UnitAddress = Convert.ToString(dr["UnitAddress"]);
                archivesDestinationRecords.Add(archivesDestinationRecord);
            }
            return archivesDestinationRecords;
        }

        public void DeleteArchivesDestinationRecord(int id)
        {
            sql = "delete from ArchivesDestinationRecord where ID=" + Convert.ToString(id);
            DB.DBHelper.ExecuteCommand(sql);
        }

        public int  ModifyArchivesDestinationRecord(ArchivesDestinationRecord archivesDestinationRecord)
        {
            int id;
            StringBuilder updateArchivesDestinationRecord = new StringBuilder();
            updateArchivesDestinationRecord.Append("update ArchivesDestinationRecord set ReceivingUnit='" + archivesDestinationRecord.ReceivingUnit
                + "',ReceivingTime='" + archivesDestinationRecord.ReceivingTime + "',ReceivingUnitPhone='" + archivesDestinationRecord.ReceivingUnitPhone
                + "',UnitAddress='" + archivesDestinationRecord.UnitAddress + "' where ID="+archivesDestinationRecord.ID);
            sql = updateArchivesDestinationRecord.ToString();
            id=DB.DBHelper.ExecuteCommand(sql);
            return id;

        }

        public int AddArchivesDestinationRecord(ArchivesDestinationRecord archivesDestinationRecord)
        {

            int id;
            StringBuilder addArchivesDestinationRecord = new StringBuilder();
            addArchivesDestinationRecord.Append("insert into ArchivesDestinationRecord(FileNumber,ReceivingUnit,ReceivingTime,ReceivingUnitPhone,UnitAddress)values('" 
                + archivesDestinationRecord.FileNumber + "','"+ archivesDestinationRecord.ReceivingUnit + "','" + archivesDestinationRecord.ReceivingTime
                + "','" + archivesDestinationRecord.ReceivingUnitPhone + "','" + archivesDestinationRecord.UnitAddress + "') select @@IDENTITY");

            sql = addArchivesDestinationRecord.ToString();
            id = DB.DBHelper.ExecuteCommand(sql);
            return id;
        }

        
        //根据档案编号得到去向ID
        public int GetIDByFileNumber(string  fileNumber)
        {
            int id = 0;
            sql = "select ID from ArchivesDestinationRecord where FileNumber="+"'"+fileNumber+"'";
            SqlDataReader dr = DB.DBHelper.Exc(sql);
            if (dr.Read())
            {
                id = Convert.ToInt32(dr["ID"]);
            }
            return id;
        }

        //根据去向ID得到档案编号
        public string  GetFileNmuerByID(int id)
        {
            string  fileNumber = string .Empty;
            sql = "select FileNumber from ArchivesDestinationRecord where ID=" + "'" + id + "'";
            SqlDataReader dr = DB.DBHelper.Exc(sql);
            if (dr.Read())
            {
                fileNumber = Convert.ToString(dr["FileNumber"]);
            }
            return fileNumber;
        }
    }
}